var Address_Prefix = "http://localhost:9200/api-person";
//判断是否需要自动登录的js
var COOKIE_NAME_AUTOLOGIN = "autoLogin";
var COOKIE_NAME = "universe";
check(); //检查自动登录的方法
function check() {
    if (document.cookie.indexOf(COOKIE_NAME_AUTOLOGIN) != -1 && getCookie(COOKIE_NAME_AUTOLOGIN) == 1 && document.cookie.indexOf(COOKIE_NAME) != -1 ) { //判断是否选取了自动登录和用户类型,个人value是1，商家为2
            console.log(32311111666666);
        // if (document.cookie.indexOf(COOKIE_NAME) !== -1) {
            console.log(getCookie(COOKIE_NAME))
            //判断保存用户信息的cookie是否存在
            $("#checkLogin").html("<p className=\"fl\" id=\"checkLogin\">\n" +
                "    <a href=\"mygrxx.html\">个人中心</a>\n" +
                "    <a onclick='DelCookie()'>注销</a>\n" +
                "    <a id='MessageCount'>消息</a>\n" +
                "</p>");

            console.log(6666);
            //取出cookie的value
            var arr = getCookie(COOKIE_NAME).split(":");
            sessionStorage.setItem("userid", arr[1]);
            sessionStorage.setItem("avatar", arr[2]);
        // }
    }else
    if(document.cookie.indexOf(COOKIE_NAME_AUTOLOGIN) !== -1 && getCookie(COOKIE_NAME_AUTOLOGIN) === 2) {
        if (document.cookie.indexOf(COOKIE_NAME) !== -1) {
            $("#checkLogin").html("<p className=\"fl\" id=\"checkLogin\">\n" +
                "    <a href=\"lessors-mygrxx.html\">商家中心</a>\n" +
                "    <a onclick='DelCookie2()'>注销</a>\n" +
                "</p>")
        }
    } else {
        checka();
    }

}

function checka() {
    if (sessionStorage.getItem("userid") != null && sessionStorage.getItem("userType") === "user") {
        $("#checkLogin").html("<p className=\"fl\" id=\"checkLogin\">\n" +
            "    <a href=\"mygrxx.html\">个人中心</a>\n" +
            "    <a onclick='DelSession()'>注销</a>\n" +
            "    <a id='MessageCount'>消息</a>\n" +
            "</p>");
    } else if (sessionStorage.getItem("busiid") != null && sessionStorage.getItem("userType") === "business") {
        $("#checkLogin").html("<p className=\"fl\" id=\"checkLogin\">\n" +
            "    <a href=\"lessors-mygrxx.html\">商家中心</a>\n" +
            "    <a onclick='DelSession2()'>注销</a>\n" +
            "</p>")
    }
}

function DelSession() {
    sessionStorage.removeItem("userid");
    location.reload();
}

function DelSession2() {
    sessionStorage.removeItem("busiid");
    location.reload();
}

//清除cookie,将过期时间改为-1 即可
function DelCookie() {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    document.cookie = COOKIE_NAME + "=" + 1 + "; expires=" + exp.toGMTString();
    DelSession();
    location.reload();
}

function DelCookie2() {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    document.cookie = COOKIE_NAME + "=" + 2 + "; expires=" + exp.toGMTString();
    DelCookie2();
    location.reload();
}

//获取cookie的value方法
function getCookie(name) {
    var prefix = name + "=";
    var start = document.cookie.indexOf(prefix);

    if (start === -1) {
        return null;
    }

    var end = document.cookie.indexOf(";", start + prefix.length)
    if (end === -1) {
        end = document.cookie.length;
    }

    var value = document.cookie.substring(start + prefix.length, end);
    return unescape(value);
}

// 初次获取,存在session中
getMessageCount();
function getMessageCount() {
    $.ajax({
        url: Address_Prefix + '/message/getMessageCount',
        dataType: 'json',
        data: {userid: sessionStorage.getItem("userid")},
        type: 'get',
        success: function (res) {
            sessionStorage.setItem("count", res.data);
            $("#MessageCount").html("消息(" + res.data + ")");
        }
    })
    return false;
}


/**
 * 轮询通知消息数量的ajax
 * @type {{data: {userid: string}, success: getting.success, dataType: string, url: string}}
 */
var getting = {
    url: Address_Prefix + '/message/getMessageCount',
    dataType: 'json',
    data: {userid: sessionStorage.getItem("userid")},
    success: function (res) {
        let count = res.data;
        if (count !== 0 && count != null) {
            if (sessionStorage.getItem("count") != count) {
                $("#MessageCount").html("消息(" + count + ")");
                sessionStorage.setItem("count", count);
            }
        }
    }
};
//关键在这里，Ajax定时访问服务端，不断获取数据 ，这里是3秒请求一次。
window.setInterval(function () {
    $.ajax(getting)
}, 3000);

